﻿// ***************************************************************
//  version:  0.4.0   date: 11/15/2007-2008
//  -------------------------------------------------------------
//  
//  -------------------------------------------------------------
//  Copyright (C) Midapex 2003-2007-2008 All Rights Reserved
// ***************************************************************
// 
// ***************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Midapex.SmartOrm
{
    public class FieldRelationTable:RelationTable
    {
        FieldInfo field;

        public FieldRelationTable(EntityColumnAttribute mcatrr, Table parent,
            Column parentPrimaryCol, FieldInfo field)
            : base( parent, parentPrimaryCol)
        {
            this.field = field;

            //设置关联表的名称
            if (mcatrr.RelationTableName == null || mcatrr.RelationTableName == string.Empty)
            {
                //默认使用(父表名称_成员名称）作为关联表的名称
                name = parent.Name + "_" + field.Name;
            }
            else
            {
                name = mcatrr.RelationTableName;
            }

            name = name.ToUpper();
        }

        public override Type ClassMemberType
        {
            get
            {
                return field.FieldType;
            }
        }

        public override object Read(object target)
        {
            return field.GetValue(target);
        }

        public override void Write(object target, object value)
        {
            field.SetValue(target, value);
        }
    }
}
